package com.yeswater.multithread.chapter7;

/**
 * author : yeswater
 * date : 2023/11/6
 *
 *  setUncaughtExceptionHandler + UncaughtExceptionHandler 进行异常处理
 */

public class Run_7_7_3 {
    public static void main(String[] args) throws InterruptedException {
        Thread1.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("线程:" + t.getName() + " 出现了异常: " );
                e.printStackTrace();
            }
        });
        Thread1 t1 = new Thread1();
        t1.setName("t1");
        t1.setUncaughtExceptionHandler((t, e) -> {
            System.out.println("线程:" + t.getName() + " 出现了异常: " );
            e.printStackTrace();
        });
        t1.start();

        Thread.sleep(5000);

        // Thread1 t2 = new Thread1();
        // t2.setName("t2");
        // t2.start();
    }

    private static class Thread1 extends Thread {
        @Override
        public void run() {
            String username = null;
            System.out.println(username.hashCode());
        }
    }
}
